//https://leetcode.cn/problems/find-k-closest-elements/
class Solution {
public:
    vector<int> findClosestElements(vector<int>& arr, int k, int x) {
        priority_queue<pair<int, int>, vector<pair<int, int>>, less<>> maxHeap;

        for (int i = 0; i < arr.size(); ++i) {
            int dist = abs(arr[i] - x);
            
            if (maxHeap.size() < k) {
                maxHeap.push({dist, arr[i]});
            } else if (dist < maxHeap.top().first) {
                maxHeap.pop();
                maxHeap.push({dist, arr[i]});
            }
        }

        vector<int> result;
        while (!maxHeap.empty()) {
            result.push_back(maxHeap.top().second);
            maxHeap.pop();
        }

        sort(result.begin(), result.end());
        return result;
    }
};
